package com.lzy.crm.service.impl;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lzy.crm.base.mybatis.BaseMapper;
import com.lzy.crm.base.mybatis.Example;
import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.service.BaseServiceImpl;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.dao.CrowdMapper;
import com.lzy.crm.dao.CrowdUserMapper;
import com.lzy.crm.dao.criteria.CrowdUserCriteria;
import com.lzy.crm.domain.Crowd;
import com.lzy.crm.domain.CrowdApply;
import com.lzy.crm.domain.CrowdUser;
import com.lzy.crm.domain.PpUser;
import com.lzy.crm.service.CrowdUserService;
import com.lzy.crm.vo.PlayerInfo;
import com.lzy.crm.wechat.consts.CrowdConstants;

@Service
public class CrowdUserServiceImpl extends
		BaseServiceImpl<CrowdUser, CrowdUserCriteria> implements
		CrowdUserService {

	@Resource
	private CrowdUserMapper crowdUserMapper;
	
	@Resource
	private CrowdMapper crowdMapper;

	
	@Override
	public BaseMapper<CrowdUser, CrowdUserCriteria> getBaseMapper() {
		return crowdUserMapper;
	}
	
	
	/**
	 * 查询群玩家
	 */
	@Override
	public List<CrowdUser> getCrowdUserList(String crowdId,String gameId, PageQuery pageQuery) {
		return crowdUserMapper.getCrowdUserList(crowdId, gameId, pageQuery);
	}
	
	
	/**
	 * 添加玩家进群
	 * @param crowdId
	 * @param user
	 */
	@Override
	public void addCrowdUser(CrowdApply crowdApply,String remark)throws Exception{
		// 判断是否是该群成员
		if(getCrowdUser(crowdApply.getCrowdId().toString() ,crowdApply.getGameId().toString())){
			return ;
		}
		// 修改牌圈人数
		Crowd crowd = crowdMapper.getById(crowdApply.getCrowdId());
		Integer playerCount = crowdUserMapper.getCrowdPlayerCount(crowdApply.getCrowdId().toString());
		crowd.setNub(playerCount+1);
		crowdMapper.updateByPrimaryKeySelective(crowd);
		
		// 添加牌圈成员
		CrowdUser crowdUser = new CrowdUser();
		crowdUser.setIsCard("0");
		crowdUser.setCrowdId(crowdApply.getCrowdId());
		crowdUser.setGameId(crowdApply.getGameId());
		crowdUser.setUnionid(crowdApply.getGameUnionid());
		crowdUser.setNickName(crowdApply.getGameNickName());
		crowdUser.setOutUserName(crowdApply.getGameNickName());
		crowdUser.setIsManager(CrowdConstants.crowd_is_manage_user);
		crowdUser.setUseNub(0);
		crowdUser.setUseNubTotal(0);
		crowdUser.setRemark(remark);
		crowdUser.setCreateTime(new java.util.Date());
		crowdUserMapper.insertSelective(crowdUser);
	}
	
	
	
	public List<CrowdUser> getCrowdUserList(String gameId){
		Example<CrowdUserCriteria> example = new Example<CrowdUserCriteria>(CrowdUserCriteria.class);
		CrowdUserCriteria crowdUserCriteria = example.createCriteria();
		crowdUserCriteria.andGameIdEqualTo(gameId);
		List<CrowdUser> crowdUserList = crowdUserMapper.getListByExample(example);
		return crowdUserList ;
	}
	
	
	/**
	 * 添加玩家进群
	 * @param crowdId
	 * @param user
	 */
	@Override
	public void addCrowdUserByPlayerInfo(String crowdId,String remark,PpUser pUser)throws Exception{
		
		// 修改牌圈人数
		Crowd crowd = crowdMapper.getById(crowdId);
		Integer playerCount = crowdUserMapper.getCrowdPlayerCount(crowdId);
		crowd.setNub(playerCount+1);
		crowdMapper.updateByPrimaryKeySelective(crowd);
		
		CrowdUser crowdUser = new CrowdUser();
		crowdUser.setCrowdId(Integer.parseInt(crowdId));
		crowdUser.setGameId(pUser.getUid());
		crowdUser.setUnionid(pUser.getUnionid());
		crowdUser.setNickName(pUser.getNickname());
		crowdUser.setOutUserName(pUser.getNickname());
		crowdUser.setIsCard("0");
		crowdUser.setUseNub(0);
		crowdUser.setRemark(remark);
		crowdUser.setIsManager(CrowdConstants.crowd_is_manage_user);
		crowdUser.setUseNubTotal(0);
		crowdUser.setCreateTime(new java.util.Date());
		
		crowdUserMapper.insertSelective(crowdUser);
	}
	
	
	/**
	 * 查询是否已经在群中
	 * @param crowdId
	 * @param gameId
	 * @return
	 */
	@Override
	public boolean getCrowdUser(String crowdId ,String gameId){
		Example<CrowdUserCriteria> example = new Example<CrowdUserCriteria>(CrowdUserCriteria.class);
		CrowdUserCriteria crowdUserCriteria = example.createCriteria();
		crowdUserCriteria.andCrowdIdEqualTo(crowdId);
		crowdUserCriteria.andGameIdEqualTo(gameId);
		List<CrowdUser> crowdUserList = crowdUserMapper.getListByExample(example);
		return crowdUserList.size()>0?true:false;
	}
	
	
	/**
	 * 查询在群中的信息
	 * @param crowdId
	 * @param gameId
	 * @return
	 */
	public CrowdUser getCrowdUserByCrowdIdAndGameId(String crowdId ,String gameId){
		Example<CrowdUserCriteria> example = new Example<CrowdUserCriteria>(CrowdUserCriteria.class);
		CrowdUserCriteria crowdUserCriteria = example.createCriteria();
		crowdUserCriteria.andCrowdIdEqualTo(crowdId);
		crowdUserCriteria.andGameIdEqualTo(gameId);
		List<CrowdUser> crowdUserList = crowdUserMapper.getListByExample(example);
		return crowdUserList.size()>0?crowdUserList.get(0):null;
	}
	
	/**
	 * 删除牌友群关系
	 * @param crowdId
	 */
	@Override
	public void delCrowdUserByCrowdId(String crowdId){
		crowdUserMapper.delCrowdUserByCrowdId(crowdId);
	}
	
	
	/**
	 * 删除牌友群关系
	 * @param crowdId
	 */
	public void delCrowdUserByCrowdIdAndGameId(String crowdId,String gameId){
		crowdUserMapper.delCrowdUserByCrowdIdAndGameId(crowdId, gameId);
	}
	
	
	/**
	 *  设置默认群信息
	 * @param user
	 * @param crowdId
	 * @param status
	 */
	@Override
	public void updateCrowdStatusDefault(PlayerInfo playerInfo,String crowdId ,String status){
		List<CrowdUser> crowdUseList = crowdUserMapper.getCrowdUserListByGameId(playerInfo.getGameId().toString());
		for(CrowdUser crowdUser:crowdUseList){
			if(StringUtil.equals(crowdId, crowdUser.getCrowdId().toString())){
				crowdUser.setIsCard(status);
				crowdUser.setRemark("1");
			}else{
				crowdUser.setIsCard("0");
				crowdUser.setRemark("0");
			}
			crowdUserMapper.updateByPrimaryKeySelective(crowdUser);
		}
	}
	
	
	// 更新群人数
	@Override
	public void updateCrowdUserCount(String crowdId){
		Example<CrowdUserCriteria> example = new Example<CrowdUserCriteria>(CrowdUserCriteria.class);
		CrowdUserCriteria crowdUserCriteria = example.createCriteria();
		crowdUserCriteria.andCrowdIdEqualTo(crowdId);
		Long playerCount= crowdUserMapper.getCountByExample(example);
		// 更新群人数
		Crowd crowd = crowdMapper.getById(crowdId);
		crowd.setNub(playerCount.intValue());
		crowdMapper.updateByPrimaryKeySelective(crowd);
	}
	
}